//平民车辆 by 略游

//平民类型

//贱民等级
asc_u_0 =
[
"C_man_p_beggar_F_asia",
"C_man_p_fugitive_F_asia",
"C_man_p_shorts_1_F_asia",
"C_Story_Mechanic_01_F",
"C_man_p_beggar_F_euro",
"C_man_p_shorts_1_F_euro",
"C_man_p_fugitive_F_euro",
"C_man_p_beggar_F_afro",
"C_man_p_shorts_1_F_afro",
"C_man_p_fugitive_F_afro",
"C_man_p_beggar_F",
"C_man_p_shorts_1_F",
"C_man_p_fugitive_F"
];

//普通公民
asc_u_1 =
[
"C_man_shorts_1_F_asia",
"C_man_polo_6_F_asia",
"C_man_polo_5_F_asia",
"C_man_polo_4_F_asia",
"C_man_polo_3_F_asia",
"C_man_polo_2_F_asia",
"C_man_polo_1_F_asia",
"C_man_shorts_2_F_asia",
"C_man_shorts_3_F_asia",
"C_man_shorts_4_F_asia",
"C_Man_casual_4_F_asia",
"C_Man_casual_5_F_asia",
"C_Man_casual_6_F_asia",
"C_man_shorts_2_F_euro",
"C_man_shorts_3_F_euro",
"C_man_shorts_4_F_euro",
"C_Man_casual_4_F_euro",
"C_Man_casual_5_F_euro",
"C_Man_casual_6_F_euro",
"C_man_polo_1_F_euro",
"C_man_polo_2_F_euro",
"C_man_polo_3_F_euro",
"C_man_polo_4_F_euro",
"C_man_polo_5_F_euro",
"C_man_polo_6_F_euro",
"C_man_shorts_1_F_euro",
"C_man_shorts_2_F_afro",
"C_man_shorts_3_F_afro",
"C_man_shorts_4_F_afro",
"C_Man_casual_4_F_afro",
"C_Man_casual_5_F_afro",
"C_Man_casual_6_F_afro",
"C_man_polo_1_F_afro",
"C_man_polo_2_F_afro",
"C_man_polo_3_F_afro",
"C_man_polo_4_F_afro",
"C_man_polo_5_F_afro",
"C_man_polo_6_F_afro",
"C_man_shorts_1_F_afro",
"C_Man_casual_6_F_tanoan",
"C_Man_casual_5_F_tanoan",
"C_Man_casual_4_F_tanoan",
"C_man_shorts_2_F",
"C_man_shorts_3_F",
"C_man_shorts_4_F",
"C_Man_casual_4_F",
"C_Man_casual_5_F",
"C_Man_casual_6_F",
"C_man_1",
"C_man_polo_1_F",
"C_man_polo_2_F",
"C_man_polo_3_F",
"C_man_polo_4_F",
"C_man_polo_5_F",
"C_man_polo_6_F",
"C_man_shorts_1_F",
"C_man_1_1_F",
"C_man_1_2_F",
"C_man_1_3_F"
];

//工农阶级
asc_u_2 =
[
"C_Man_Messenger_01_F",
"C_Man_ParamediC_01_F",
"C_man_w_worker_F",
"C_Man_ConstructionWorker_01_Red_F",
"C_Man_ConstructionWorker_01_Blue_F",
"C_Man_ConstructionWorker_01_Black_F",
"C_Man_ConstructionWorker_01_Vrana_F",
"C_Marshal_F",
"C_Man_UtilityWorker_01_F",
"C_Man_Fisherman_01_F",
"C_man_hunter_1_F",
"C_scientist_F",
"C_Journalist_01_War_F",
"C_journalist_F",
"C_man_pilot_F",
"C_Man_UAV_06_F",
"C_Man_UAV_06_medical_F"
];

//资产阶级
asc_u_3 =
[
"C_man_sport_1_F_asia",
"C_man_sport_2_F_asia",
"C_man_sport_3_F_asia",
"C_Man_casual_3_F_asia",
"C_Man_casual_1_F_asia",
"C_Man_casual_2_F_asia",
"C_Nikos",
"C_Orestes",
"C_Man_casual_1_F_euro",
"C_Man_casual_2_F_euro",
"C_Man_casual_3_F_euro",
"C_man_sport_1_F_euro",
"C_man_sport_2_F_euro",
"C_man_sport_3_F_euro",
"C_Man_casual_1_F_afro",
"C_Man_casual_2_F_afro",
"C_Man_casual_3_F_afro",
"C_man_sport_1_F_afro",
"C_man_sport_2_F_afro",
"C_man_sport_3_F_afro",
"C_man_sport_3_F_tanoan",
"C_man_sport_2_F_tanoan",
"C_man_sport_1_F_tanoan",
"C_Man_casual_3_F_tanoan",
"C_Man_casual_2_F_tanoan",
"C_Man_casual_1_F_tanoan",
"C_Man_casual_1_F",
"C_Man_casual_2_F",
"C_Man_casual_3_F",
"C_man_sport_1_F",
"C_man_sport_2_F",
"C_man_sport_3_F",
"C_Driver_3_F",
"C_Driver_1_F",
"C_Driver_2_F",
"C_Driver_4_F"
];


if(Param_Version >= 183) then
{
	//外星人dlc
	asc_u_0 pushBack "C_Farmer_01_enoch_F";

	asc_u_1 pushBack "C_Man_1_enoch_F";
	asc_u_1 pushBack "C_Man_4_enoch_F";

	asc_u_2 pushBack "C_Man_5_enoch_F";
	asc_u_2 pushBack "C_Man_6_enoch_F";

	asc_u_3 pushBack "C_Man_2_enoch_F";
	asc_u_3 pushBack "C_Man_3_enoch_F";
	
	//红十字dlc
	asc_u_1 append [
"C_Man_casual_4_v2_F_asia",
"C_Man_casual_5_v2_F_asia",
"C_Man_casual_6_v2_F_asia",
"C_Man_casual_7_F_asia",
"C_Man_casual_8_F_asia",
"C_Man_casual_9_F_asia",
"C_Man_casual_4_v2_F_euro",
"C_Man_casual_5_v2_F_euro",
"C_Man_casual_6_v2_F_euro",
"C_Man_casual_7_F_euro",
"C_Man_casual_8_F_euro",
"C_Man_casual_9_F_euro",
"C_Man_casual_4_v2_F_afro",
"C_Man_casual_5_v2_F_afro",
"C_Man_casual_6_v2_F_afro",
"C_Man_casual_7_F_afro",
"C_Man_casual_8_F_afro",
"C_Man_casual_9_F_afro",
"C_Man_casual_4_v2_F_tanoan",
"C_Man_casual_5_v2_F_tanoan",
"C_Man_casual_6_v2_F_tanoan",
"C_Man_casual_7_F_tanoan",
"C_Man_casual_8_F_tanoan",
"C_Man_casual_9_F_tanoan",
"C_Man_casual_4_v2_F",
"C_Man_casual_5_v2_F",
"C_Man_casual_6_v2_F",
"C_Man_casual_7_F",
"C_Man_casual_8_F",
"C_Man_casual_9_F"];
	
	asc_u_2 append [
"C_Man_formal_1_F_asia",
"C_Man_formal_2_F_asia",
"C_Man_formal_3_F_asia",
"C_Man_formal_4_F_asia",
"C_Man_formal_1_F_euro",
"C_Man_formal_2_F_euro",
"C_Man_formal_3_F_euro",
"C_Man_formal_4_F_euro",
"C_Man_formal_1_F_afro",
"C_Man_formal_2_F_afro",
"C_Man_formal_3_F_afro",
"C_Man_formal_4_F_afro",
"C_Man_formal_2_F_tanoan",
"C_Man_formal_3_F_tanoan",
"C_Man_formal_4_F_tanoan",
"C_Man_formal_1_F",
"C_Man_formal_2_F",
"C_Man_formal_3_F",
"C_Man_formal_4_F",
"C_Man_formal_1_F_tanoan"
];
	
	asc_u_3 append [
"C_Man_smart_casual_1_F_asia",
"C_Man_smart_casual_2_F_asia",
"C_Man_smart_casual_1_F_euro",
"C_Man_smart_casual_2_F_euro",
"C_Man_smart_casual_1_F_afro",
"C_Man_smart_casual_2_F_afro",
"C_Man_smart_casual_1_F_tanoan",
"C_Man_smart_casual_2_F_tanoan",
"C_Man_smart_casual_1_F",
"C_Man_smart_casual_2_F"];
};

//相关载具
asc_v_0 =
[
"C_Van_01_transport_F",
"C_Van_01_box_F",
"C_Offroad_01_F",
"C_Van_02_transport_F",
"C_Van_02_vehicle_F",
"C_Truck_02_transport_F"
];


asc_v_1 =
[
"C_Quadbike_01_F",
"C_Hatchback_01_sport_F",
"C_Hatchback_01_F",
"C_Offroad_02_unarmed_F",
"C_Truck_02_covered_F",
"C_IDAP_Offroad_01_F"
];

asc_v_2 =
[
"C_Offroad_01_repair_F",
"C_Van_02_medevac_F",
"C_Van_02_service_F",
"C_Truck_02_fuel_F",
"C_Truck_02_box_F",
"C_IDAP_Truck_02_transport_F",
"C_IDAP_Truck_02_F",
"C_IDAP_Truck_02_water_F",
"C_IDAP_Van_02_vehicle_F",
"C_IDAP_Van_02_medevac_F",
"C_IDAP_Van_02_transport_F",
"C_Van_01_fuel_F"
];

asc_v_3 =
[
"C_SUV_01_F",
"C_IDAP_Offroad_02_unarmed_F",
"C_Kart_01_Red_F",
"C_Kart_01_Fuel_F",
"C_Kart_01_F",
"C_Kart_01_Blu_F",
"C_Kart_01_Vrana_F"
];

if(Param_Version >= 183) then
{
	asc_v_1 pushBack "C_Tractor_01_F";
	asc_v_2 pushBack "C_Offroad_01_covered_F";
	asc_v_3 pushBack "C_Offroad_01_comms_F";
};


as_civ_num = 0;

//总体逻辑：一旦实体车辆脱离范围，就一定清除掉(1500)
//首先车辆朝玩家附近的地点移动(300)，如果没有就往road移动(300)
fnc_clearCiv =
{
	params ["_veh", "_group"];
	{
		deleteVehicle _x;
	}forEach (units _group);
	deleteGroup _group;
	deleteVehicle _veh;
};

fnc_controlCiv =
{
	private ["_veh", "_group","_locs", "_roads", "_end", "_count", "_pos_last"];
	_veh = _this select 0;
	_group = group driver _veh;
	_end = false;
	_count = 0;//不动计数
	_pos_last = position _veh;
	as_civ_num = as_civ_num + 1;
	
	
	while {!_end} do
	{
		//载具没了，或爆了，或没人，或不是平民
		if(isNull _veh) exitWith{[_veh, _group] call fnc_clearCiv};
		if(!alive _veh) exitWith{[_veh, _group] call fnc_clearCiv};
		private _driver = driver _veh;
		//人车分离，但是车还在。则开启动态模拟，如果还远离就删除
		if(isNull _driver || {!alive _driver} || {side _driver != civilian}) exitWith
		{
			_veh enableDynamicSimulation true;
			_group enableDynamicSimulation true;
			
			//所有人下车
			{ unassignVehicle _x } forEach crew _veh; crew _veh allowGetIn false;
			
			//远离玩家删除
			[_veh, _group] spawn
			{
				params ["_veh", "_group"];
				while{true} do
				{
					sleep 30;
					private _clean = true;
					{
						if(_x distance _veh < 600) exitWith
						{
							_clean = false;
						};
					}forEach AllPlayers;
					if(_clean) exitWith {[_veh, _group] call fnc_clearCiv};
				};
			};		
		};
		
		//不动一分钟也删除
		if(_pos_last distance2D (position _veh) < 50) then
		{
			_count = _count + 1;
		}else
		{
			_count = 0;
			_pos_last = position _veh;
		};
		if(_count >= 6) exitWith{[_veh, _group] call fnc_clearCiv};
		
		//远离玩家删除
		private _clean = true;
		{
			if(_x distance _veh < 1800) exitWith
			{
				_clean = false;
			};
		}forEach AllPlayers;
		if(_clean) exitWith {[_veh, _group] call fnc_clearCiv};

		
		
		//等待路点
		if( (count waypoints _group) <= 1) then
		{
			//添加随机地点作为新路点
			_locs = nearestLocations [position _veh, ["NameCityCapital","NameCity","NameVillage","NameLocal"], 2800];
			if((count _locs) <= 1) then 
			{
				//如果没有就往玩家附近的路开
				private _player = objNull;
				//没倒的玩家
				private _list =  ([] call BIS_fnc_listPlayers) select {alive _x && (_x getVariable ['FAR_isUnconscious',1] == 0)};
				{
					if((_x distance2D _veh) < 2800) exitWith
					{
						_player = _x;
					};
					
				}forEach _list;
				if(isNull _player) then{_end = true;[_veh, _group] call fnc_clearCiv}
				else
				{
					[_group, _player, 200] call fn_addWaypoint;
				};
			}
			else
			{
				_locs deleteAt 0;
				[_group, locationPosition (selectRandom _locs), 50] call fn_addWaypoint;
			};
		};
		sleep 10;
	};
	
	as_civ_num = as_civ_num - 1;
};
//
private ["_t", "_locs", "_roads", "_pos", "_veh", "_group", "_unit1", "_crew_num"];
while {true} do
{
	waitUntil {sleep 10;as_civ_num < 12};
	_t = 33;
	{
		if(isNull _x || {!alive _x}) then
		{
		}else
		{
			//创建车
			_locs = [];
			_locs = nearestLocations [position _x, ["NameCityCapital","NameCity","NameVillage","NameLocal"], 1500];
			
			//筛选离玩家太近的
			_locs = _locs select
			{
				private _locPos = locationPosition _x;
				private _ret = true;
				{
					if(_x distance2D _locPos < 300) exitWith
					{
						_ret = false;
					}
				}forEach AllPlayers;
				_ret
			};
			
			if(count _locs > 0) then
			{
				_pos = locationPosition (selectRandom _locs);
				//选择道路（附近无载具建筑）并且无玩家
				_roads = [];
				_roads = (_pos nearRoads 300) select
				{
					private _nearObj = [];
					_nearObj = nearestObjects [position _x, ["Car","Tank","Air","House", "Building"], 2];
					((count _nearObj) == 0)
				};
				if(((count _roads) == 0)) then
				{
					_pos = _pos getPos[300, random 360];
				}
				else
				{
					_pos = position (_roads select 0);
				};
				//创建组
				_group = createGroup civilian;
				if(Param_Debug == 1) then
				{
					_group addGroupIcon ["b_inf",[0,0]];
					_group setGroupIconParams [[1,0,1,1],"平民车辆",1,true];
				};
				//创建单位
				_r = random 100;
				
				if(_r < 35) then
				{
					_veh = createVehicle [selectRandom asc_v_0, _pos , [], 0, "NONE"];
					_crew_num = ceil random count fullCrew [_veh, "", true];
					if(_crew_num > 3) then{_crew_num = 3;};
					for ("_i") from 1 to _crew_num do {
						_unit1 = [selectRandom asc_u_0, _pos, 0, _group] call fn_createUnit;
						_unit1 moveInAny _veh;
						//玩家杀平民计数
						_unit1 addEventHandler ["killed", 
						{
							private _killer = _this select 1;
							if(isPlayer _killer) then{as_civ_kill_num = as_civ_kill_num + 1;}
						}];
						_unit1 setVariable["as_money", ceil(random 5), true];
					};
				}
				else
				{
					if(_r < 60) then
					{
						_veh = createVehicle [selectRandom asc_v_1, _pos , [], 0, "NONE"];
						_crew_num = ceil random count fullCrew [_veh, "", true];
						if(_crew_num > 3) then{_crew_num = 3;};
						for ("_i") from 1 to _crew_num do {
							_unit1 = [selectRandom asc_u_1, _pos, 0, _group] call fn_createUnit;
							_unit1 moveInAny _veh;
							//玩家杀平民计数
							_unit1 addEventHandler ["killed", 
							{
								private _killer = _this select 1;
								if(isPlayer _killer) then{as_civ_kill_num = as_civ_kill_num + 1;}
							}];
							_unit1 setVariable["as_money", ceil(random 20), true];
						};
					}
					else
					{
						if(_r < 80) then
						{
							_veh = createVehicle [selectRandom asc_v_2, _pos , [], 0, "NONE"];
							_crew_num = ceil random count fullCrew [_veh, "", true];
							if(_crew_num > 3) then{_crew_num = 3;};
							for ("_i") from 1 to _crew_num do {
								_unit1 = [selectRandom asc_u_2, _pos, 0, _group] call fn_createUnit;
								_unit1 moveInAny _veh;
								//玩家杀平民计数
								_unit1 addEventHandler ["killed", 
								{
									private _killer = _this select 1;
									if(isPlayer _killer) then{as_civ_kill_num = as_civ_kill_num + 1;}
								}];
								_unit1 setVariable["as_money", ceil(random 50), true];
							};
						}
						else
						{
							_veh = createVehicle [selectRandom asc_v_3, _pos , [], 0, "NONE"];
							_crew_num = ceil random count fullCrew [_veh, "", true];
							if(_crew_num > 3) then{_crew_num = 3;};
							for ("_i") from 1 to _crew_num  do {
								_unit1 = [selectRandom asc_u_3, _pos, 0, _group] call fn_createUnit;
								_unit1 moveInAny _veh;
								//玩家杀平民计数
								_unit1 addEventHandler ["killed", 
								{
									private _killer = _this select 1;
									if(isPlayer _killer) then{as_civ_kill_num = as_civ_kill_num + 1;}
								}];
								_unit1 setVariable["as_money", ceil(random 100), true];
							};
						};
					};
				};
				
				
				_veh engineOn true;
				[_veh] spawn fnc_controlCiv;
				//概率双倍延迟
				if(random 100 < 50) then
				{
					Sleep _t * 2;
				}
				else
				{
					Sleep _t;//延迟
				};
				
			};
		};
		
		
	}forEach AllPlayers;
	sleep 5;
};
